<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
namespace addon\mall\logic;
use app\common\logic\LogicBase;
use \tpfcore\Core;
use \tpfcore\util\Tree;

class GoodsCategory extends LogicBase
{
	public function getGoodsCategory($data=[]){
		return self::getList($data);
	}
	public function getListTree($data=[]){
		$result=self::getList($data);
		$new_result=[];
		foreach ($result as $key => $value) {
			$new_result[$key]=$value->toArray();
		}
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($new_result as $n=> $r) {
        	$new_result[$n]['parentid_node'] = ($r['parent_id']) ? ' class="child-of-node-' . $r['parent_id'] . ' collapsed"' : 'class="expanded"';
            $new_result[$n]['manage'] = '<a href="' . url("mall/GoodsCategory/add", ["parentid" => $r['id']]) . '">添加分类</a> | <a href="' . url("mall/GoodsCategory/add", ["id" => $r['id']]) . '">编辑分类</a> | <a href="' . url("mall/GoodsCategory/del", ["id" => $r['id']]). '" class="js-ajax-dialog-btn" data-msg="您确定要删除此数据吗？">删除分类</a> ';
            $new_result[$n]['display'] = $r['display'] ? '<img src="/data/assets/images/btn_enable.png" title="显示" class="ajax-status" data="GoodsCategory|display|id|'.$r['id'].'" action="'.url('mall/GoodsCategory/ajaxdata').'">' : '<img src="/data/assets/images/btn_disable.gif" title="不显示" class="ajax-status" data="GoodsCategory|display|id|'.$r['id'].'" action="'.url('mall/GoodsCategory/ajaxdata').'">';
        }
       	
       	$url=url('mall/GoodsCategory/ajaxdata');
        $tree->init($new_result);
        $tree->parentid="parent_id";
        $str = "<tr id='node-\$id' \$parentid_node>
					<td>\$id</td>
					<td>\$spacer\$name</td>
					<td style='padding-left:20px;'><input name='listorders[\$id]' data='GoodsCategory|sort|id|\$id' type='text' size='3' value='\$sort' action='$url' class='input input-order ajax-text'></td>
				    <td>\$display</td>
				    <td>\$keywords</td>
					<td>\$manage</td>
				</tr>";
        $categorys = $tree->get_tree(0, $str);
		return $categorys;
	}
	public function getTreeGoodsCategory($data=[]){
		$parentid=0;
		// 添加分类时传递
		if(isset($data['parentid']) && is_numeric($data['parentid']) && $data['parentid']>0){
			$parentid=$data['parentid'];
			unset($data['parentid']);
		}
		$result=self::getList($data);
		$new_result=[];
		foreach ($result as $key => $value) {
			$new_result[$value['id']]=$value->toArray();
		}
		
        $tree = new Tree();
        $tree->parentid="parent_id";
        $tree->init($new_result);
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $categorys = $tree->get_tree(0, $str,$parentid);
		return $categorys;
	}
	public function saveGoodsCategory($data){
		$validate=Core::addonValidate($this->name);
		$scene = isset($data['id'])?"edit":"add";
        $validate_result = $validate->scene($scene)->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $validate->getError(), null];
        }
//        print_r($data);
        $result = Core::loadAddonModel($this->name)->saveObject($data);
		if($result){
			return [RESULT_SUCCESS,"操作成功",url('mall/GoodsCategory/index')];
		}
		return [RESULT_ERROR,"操作失败"]; 
	}
	/*
		有以下两种情况都不允许删除
		1、产品表有分类关联，包括子分类关联
		2、分类下还有子分类
	*/
	public function delGoodsCategory($data){
		if(self::getStatistics(["parent_id"=>$data['id']])>0){
			return [RESULT_ERROR,"该分类下还有子分类，不允许删除"]; 
		}

		if(Core::loadAddonModel("Goods")->getStatistics(['category_id'=>$data['id']])>0){
			return [RESULT_ERROR,"该分类下还有产品，不允许删除"];
		}

		$result = self::deleteObject($data,true);
		if($result){
			return [RESULT_SUCCESS,"操作成功",url('mall/GoodsCategory/index')];
		}
		return [RESULT_ERROR,"操作失败"]; 
	}

	public function getChilds($parentid=0){
		if($list=self::getList(["where"=>['parent_id'=>$parentid]]) && $list){
			$this->ids=$list[0]->id;
			$this->getChilds($list[0]->id);
		}
	}

}